<?php
/*
 * 手机客户端 秀场展品详细接口
 * 传入参数 
 * 2013-12-24
 */
include "inc/public_header.php";
include "inc/MiniaturePro.class.php";
include "func/user.php";	//用户信息处理函数
include "func/pic.php";		//图片处理函数

$MAX_IMAGE_WIDTH = 1000;	//最大图片宽度

$reJson = array();//构建返回值

$worksid	     	= $_REQUEST["worksid"];	//作品id
$width				= $_REQUEST["width"];	//手机屏幕宽度
//$hd_id				= $_REQUEST["hd_id"];	//活动主键

$reJson["success"] = "1";

//获取用户信息
$uInfoArr = user_getInfoFromUUID();
if(!$uInfoArr){
	$reJson["havepraised"] = "-1";
}else{
	$uid = $uInfoArr["uid"];//用户信息主键
	$sql = "select id from new_weituo_pingfen where zpid='".$worksid."' and uid='".$uid."' and zan='1'";
	$db->query($sql);
	if($db->next_record()){
		$reJson["havepraised"] = "1";
	}else{
		$reJson["havepraised"] = "0";
	}
}

$miniature = new MiniaturePro();	//构建缩略图处理类
$imgArr = array();
$hasData = false;

//获取作品所有图片
$sql = "select 
				 a.goods_pic_url
				,b.czln 
				,e.id as mini_img_id
				,e.awidth as mini_img_width
				,e.aheight as mini_img_height
				,e.aname as mini_img_aname
				,a.id
		from 
				new_weituo_pic a 
				inner join new_weituo_zuopin b on a.zpid=b.id 
				left join new_weituo_pic_size e on a.id=e.img_id and e.awidth='".$MAX_IMAGE_WIDTH."'
		where 
				b.id='".$worksid."' and b.dr='0'";

if(isset($uid) && $uid!=""){
	$sql = $sql." and (b.uid='".$uid."' or b.aflag='1')";
}else{
	$sql = $sql." and b.aflag='1' ";
}
$db->query($sql);
$imgInfoArr = array();
while($db->next_record()){
	//处理生成缩略图
	$imgInfos = pic_resize(
							 $miniature
							,$db->f("goods_pic_url")
							,$db->f("mini_img_aname")
							,$MAX_IMAGE_WIDTH
							,$db->f("id")
							,$db->f("mini_img_id")
							,$db->f("mini_img_width")
							,$db->f("mini_img_height"));
	if($imgInfos["success"]=="0"){
		//图片有问题，无法显示
		$imgInfos["mini_img_url"] = "";
	}
	$imgInfoArr[$db->f("id")]["1000"] = $imgInfos["mini_img_url"];
}


//获取作品所有图片
$sql = "select 
				 a.goods_pic_url
				,b.czln 
				,e.id as mini_img_id
				,e.awidth as mini_img_width
				,e.aheight as mini_img_height
				,e.aname as mini_img_aname
				,a.id
		from 
				new_weituo_pic a 
				inner join new_weituo_zuopin b on a.zpid=b.id 
				left join new_weituo_pic_size e on a.id=e.img_id and e.awidth='640'
		where 
				b.id='".$worksid."' and b.dr='0'";

if(isset($uid) && $uid!=""){
	$sql = $sql." and (b.uid='".$uid."' or b.aflag='1')";
}else{
	$sql = $sql." and b.aflag='1' ";
}
$db->query($sql);

$miniature = new MiniaturePro();	//构建缩略图处理类
$imgArr = array();
$hasData = false;
while($db->next_record()){
	//处理生成缩略图
	$imgInfos = pic_resize(
							 $miniature
							,$db->f("goods_pic_url")
							,$db->f("mini_img_aname")
							,640
							,$db->f("id")
							,$db->f("mini_img_id")
							,$db->f("mini_img_width")
							,$db->f("mini_img_height"));
	if($imgInfos["success"]=="0"){
		//图片有问题，无法显示
		$imgInfos["mini_img_url"] = "";
	}
	$imgInfoArr[$db->f("id")]["640"] = $imgInfos["mini_img_url"];
}
foreach((array)$imgInfoArr as $i=>$v){
	$sImg = "";
	$bImg = "";
	if($imgInfoArr[$i]["640"]){
		$sImg = $imgInfoArr[$i]["640"];
	}
	if($imgInfoArr[$i]["1000"]){
		$bImg = $imgInfoArr[$i]["1000"];
	}
	$imgArr[]=array(
		"bigImgUrl"=> $bImg//输出这个作品的所有图片
		,"imgUrl"=>$sImg
	);
	$hasData = true;
}

	
if(!$hasData){
	//如果没数据，说明该用户访问的是未审核的作品
	echo json_encode(array("success"=>"0","errCode"=>"1","msg"=>"没有获取到作品信息，可能作品未审核"));
	die;
}
$reJson["list"] = $imgArr;

//获取创作理念
$sql="
		select 
				czln
				,b.ct as zan_count
				,c.ct as pl_count
				,d.ct as gz_count
		from 
				new_weituo_zuopin a
				left join (select count(0) as ct,zpid from new_weituo_pingfen where zan='1' group by zpid) b on a.id=b.zpid
				left join (select count(0) as ct,zpid from new_weituo_comment where dr='0' group by zpid) c on a.id=c.zpid 
				left join (select count(0) as ct,zpid from new_weituo_gz group by zpid) d on a.id=d.zpid
		where
				id=".$worksid."
";
$db->query($sql);
while($db->next_record()){
	if($db->f("czln")){
		$reJson["ideas"] = $db->f("czln"); 				//创作理念
	}else{
		$reJson["ideas"] = "";
	}
	$reJson["praiseCount"] = $db->f("zan_count")?$db->f("zan_count"):"0"; 		//赞总数
	$reJson["commentCount"] = $db->f("pl_count")?$db->f("pl_count"):"0"; 		//评论总数
	$reJson["collectionCount"] = $db->f("gz_count")?$db->f("gz_count"):"0";	//收藏总数
}

//判断该用户有没有收藏过
$sql = "select count(0) as ct from new_weituo_gz where zpid='".$worksid."' and uid='".$uid."'";
$db->query($sql);
$db->next_record();
if(((int)$db->f("ct"))>0){
	$reJson["haveCollected"] = "1"; //收藏过
}else{
	$reJson["haveCollected"] = "0"; //为收藏
}
echo json_encode($reJson);
?>